package com.smartum.mpr.dao;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.smartum.mpr.exception.DatabaseIntegrityException;
import com.smartum.mpr.exception.NotCorrectPermission;
import com.smartum.mpr.exception.NotExistsSadException;
import com.smartum.mpr.model.Sad;
import com.smartum.mpr.model.UploadedImageSad;

@Repository
public class UploadedImageSadDao extends GenericDao<Long, UploadedImageSad> {


	public Sad getSad(Long userId, Long imageId) throws NotExistsSadException, DatabaseIntegrityException{
		DetachedCriteria criteria = DetachedCriteria.forClass(UploadedImageSad.class);
		criteria.createCriteria("user").add(Restrictions.eq("id", userId));
		criteria.createCriteria("image").add(Restrictions.eq("id", imageId));

		List sads = hibernateTemplate.findByCriteria(criteria);

		if (sads.size() == 0) {
			throw new NotExistsSadException();
		} else if (sads.size() > 1) {
			throw new DatabaseIntegrityException("database has few equals sads");
		} else if (sads.size() == 1) {
			return (Sad) sads.get(0);
		} else {
			throw new DatabaseIntegrityException();
		}
	}

	@Override
	protected Class<UploadedImageSad> getEntityClass() {
		return UploadedImageSad.class;
	}

}
